package com.fangzhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fangzhi.mapper.AdminAccountRoleDeptRelationMapper;
import com.fangzhi.mapper.LogsMapper;
import com.fangzhi.mapper.OrdersMapper;
import com.fangzhi.mobel.AdminAccountRoleDeptRelation;
import com.fangzhi.mobel.Logs;
import com.fangzhi.mobel.OrderPayment;
import com.fangzhi.mobel.Orders;
import com.fangzhi.mobel.enums.GlobalCodeEnum;
import com.fangzhi.mobel.io.SaveOrderIo;
import com.fangzhi.mobel.io.SavePaymentIo;
import com.fangzhi.mobel.vo.LoginVo;
import com.fangzhi.service.OrderPaymentService;
import com.fangzhi.mapper.OrderPaymentMapper;
import com.fangzhi.service.OrdersService;
import com.fangzhi.utils.GlobalParam;
import com.fangzhi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author edy
* @description 针对表【order_payment】的数据库操作Service实现
* @createDate 2024-09-18 14:12:43
*/
@Service
public class OrderPaymentServiceImpl extends ServiceImpl<OrderPaymentMapper, OrderPayment>
    implements OrderPaymentService{

    @Autowired
    private LogsMapper logsMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private AdminAccountRoleDeptRelationMapper adminAccountRoleDeptRelationMapper;

    @Override
    public Integer saveByOrderId(String orderId, SaveOrderIo.PaymenyIo paymenyIo) {
        if (null == paymenyIo || paymenyIo.getPrice().intValue() == 0)
            return 0;

        BigDecimal price = paymenyIo.getPrice().multiply(BigDecimal.valueOf(100));
        OrderPayment orderPayment = new OrderPayment().setOrderId(orderId).setPrice(price.intValue())
                .setImageCover(paymenyIo.getImageCover()).setType(1).setComments(paymenyIo.getComments());
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        AdminAccountRoleDeptRelation adminAccountRoleDeptRelation = adminAccountRoleDeptRelationMapper.selectOne(Wrappers.<AdminAccountRoleDeptRelation>lambdaQuery()
                .eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId()));
        if (adminAccountRoleDeptRelation.getRoleId() == 2)
            orderPayment.setStatus(2);

        boolean save = this.save(orderPayment);
        if (save){
            logsMapper.insert(new Logs().setType(2).setType2(1).setAboutId(orderPayment.getId()).setAdminAccountId(loginVo.getId()));
            return price.intValue();
        }

        throw new RuntimeException("新增订单定金失败");
    }

    @Override
    public ResponseResult toExists(Integer id, Integer needStatus, Long roleId, Integer pass) {
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        OrderPayment orderPayment = this.getBaseMapper().selectById(id);
        if (orderPayment.getStatus() != needStatus)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9996);

        orderPayment.setStatus(needStatus + 1);
        if (pass == 2)
            orderPayment.setStatus(4);
        int i = this.getBaseMapper().updateById(orderPayment);
        if (i == 1){
            Logs logs = new Logs().setType(1).setType2(2).setAboutId(orderPayment.getId()).setAdminAccountId(loginVo.getId()).setType3(1);
            if (pass == 2)
                logs.setType3(2);
            logsMapper.insert(logs);
            if (roleId == 5L){
                Orders orders = ordersMapper.selectOne(Wrappers.<Orders>lambdaQuery().eq(Orders::getOrderId, orderPayment.getOrderId()));
                orders.setDepositAmount(orders.getDepositAmount() + orderPayment.getPrice());
                if (orders.getTotalAmount() <= (orders.getDepositAmount() + orders.getAfterAmount() + orders.getDiscountAmount()))
                    orders.setStatus(5);
                ordersMapper.updateById(orders);
            }
            return ResponseResult.OK();
        }
       throw new RuntimeException("回款审核失败");
    }

    @Override
    public ResponseResult savePayment(SavePaymentIo io) {
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        AdminAccountRoleDeptRelation adminAccountRoleDeptRelation = adminAccountRoleDeptRelationMapper.selectOne(Wrappers.<AdminAccountRoleDeptRelation>lambdaQuery()
                .eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId()));

        Orders orders = ordersMapper.selectOne(Wrappers.<Orders>lambdaQuery().eq(Orders::getOrderId, io.getOrderId()));
        if (null == orders || !orders.getAdminAccountId().equals(loginVo.getId()))
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9996);

        if (orders.getStatus() != 4)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9993);

        BigDecimal price = io.getPrice().multiply(BigDecimal.valueOf(100));
        OrderPayment orderPayment = new OrderPayment().setOrderId(orders.getOrderId()).setPrice(price.intValue())
                .setImageCover(io.getImageCover()).setComments(io.getComments()).setType(2).setStatus(3);
//        if (adminAccountRoleDeptRelation.getRoleId() == 3){
//            orderPayment.setStatus(1);
//        }else  if (adminAccountRoleDeptRelation.getRoleId() == 2){
//            orderPayment.setStatus(2);
//        }
        boolean save = this.save(orderPayment);
        if (save){
            ordersMapper.updateById(orders.setDepositAmount(orders.getDepositAmount() +  price.intValue()));
            logsMapper.insert(new Logs().setType(2).setType2(1).setAboutId(orderPayment.getId()).setAdminAccountId(loginVo.getId()).setType3(2));
            return ResponseResult.OK();
        }
        throw new RuntimeException("新增回款失败" + JSON.toJSONString(io));
    }
}




